const prefix = `/api`;
import Mock from "mockjs";

const tokens: any = {
  admin: {
    password: 123456,
    token: "admin-token",
  },
  editor: {
    password: 123456,
    token: "editor-token",
  },
};

// get请求从config.url获取参数，post从config.body中获取参数
function paramObj(url: string) {
  const search = url.split("?")[1];
  if (!search) {
    return {};
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
      '"}'
  );
}

const auth = [
  // 登陆接口
  {
    url: `${prefix}/user/login`,
    type: "get",
    response: (config: { url: any }) => {
      const { username } = paramObj(config.url);
      const token = tokens[username].token;
      if (token) {
        return {
          code: 0,
          message: "操作成功",
          data: {
            token: token,
          },
        };
      }
      return {
        code: 1,
        message: "用户信息错误，请重试~",
        data: {},
      };
    },
  },
  // getperms
  {
    url: `${prefix}/account/getperms`,
    type: "get",
    response: () => {
      return {
        code: 0,
        message: "操作成功",
        data: {
          user: Mock.mock({
            email: "@EMAIL",
            name: "@NAME",
            phone: "@PHONE",
          }),
          menus: [
            {
              path: "/sys/user",
              name: "SysUserPage",
              title: "用户管理",
              open: false,
              hidden: false,
              target: "_self",
              isLeaf: true,
              icon: "",
            },
            {
              path: "/sys/dept",
              name: "SysDeptPage",
              title: "组织管理",
              open: false,
              hidden: false,
              target: "_self",
              isLeaf: true,
              icon: "",
            },
          ],
          perms: [],
        },
      };
    },
  },
  // 用户信息
  {
    url: `${prefix}/admin/user`,
    type: "get",
    response: () => {
      return {
        code: 0,
        message: "操作成功",
        data: Mock.mock({
          email: "@EMAIL",
          name: "@NAME",
          phone: "@PHONE",
        }),
      };
    },
  },
  // 用户菜单
  {
    url: `${prefix}/admin/menu`,
    type: "get",
    response: () => {
      return {
        code: 0,
        message: "操作成功",
        data: [
          {
            a: "ss",
          },
        ],
      };
    },
  },
];
export default auth;
